package com.chenjun.web.controller.atten;

import java.lang.reflect.InvocationTargetException;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chenjun.common.base.ResultDto;
import com.chenjun.common.bean.Pagination;
import com.chenjun.common.model.DataGridModel;
import com.chenjun.common.model.DataGridSortModel;
import com.chenjun.core.dao.generator.entity.OaDictAttnEqpt;
import com.chenjun.core.dao.generator.entity.SysUser;
import com.chenjun.core.service.atten.AttendanceEquipmentService;
import com.chenjun.core.service.sys.SysUserService;
import com.chenjun.core.vo.atten.AttnEqptDto;
import com.chenjun.web.controller.BaseController;

/**
 * 
 * @Description: 考勤设备管理 
 * @author yanjinyin
 * @date 2017年8月29日 下午9:40:52
 */
@Controller
@RequestMapping("attendanceEquipment")
public class AttendanceEquipmentController extends BaseController {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Resource
	private AttendanceEquipmentService attenEquiService;
	
	@Resource
	private SysUserService userService;
	
	
	/**
	 * 
	 * @Description: 进入"考勤设备管理 "页面
	 * @return
	 * @author yanjinyin
	 * @date 2017年8月29日 下午10:07:14
	 */
	@RequestMapping("/list.html")
	public String index() {
		return "attendanceManagement/attendanceEquipment";
	}
	
	/**
	 * 分页搜索
	 * @param datagrid
	 * @return
	 * ResultDto
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	@RequestMapping(value = "/search")
	@ResponseBody
	public ResultDto list(DataGridModel datagrid) {
		ResultDto rtDto = new ResultDto();
		datagrid.getSortBy().add(new DataGridSortModel("create_time", DataGridSortModel.DESC));
		Pagination data = attenEquiService.doPagination(datagrid);
		rtDto.setData(data);
		return rtDto;
	}
	
	/**
	 * 编辑页面元素
	 * @return
	 * ResultDto
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public ResultDto edit(@Valid AttnEqptDto dto,Errors errors)  throws IllegalAccessException, InvocationTargetException{
		ResultDto rtDto = new ResultDto();
		
		//验证
		if (!hanleErrors(rtDto, errors)) { return rtDto; }
		
		OaDictAttnEqpt ae = AttnEqptDto.getModelByDto(dto);
		SysUser user = userService.get(ae.getGatherUserId());
		if(user == null) {
			rtDto.resultError();
			rtDto.setMessage("未找到对应员工号");
			return rtDto;
		}
		ae.setGatherUserName(user.getUserName());
		int i = attenEquiService.createOrUpdate(ae);
		if(i<=0){
			rtDto.resultError();
			rtDto.setMessage("编辑系统异常");
		}
		return rtDto;
	}

	/**
	 * 删除指定的记录
	 * @param id
	 * @return
	 * ResultDto
	 */
	@RequestMapping("/delete/{id}")
	@ResponseBody
	public ResultDto delete(@PathVariable Integer id) {
		ResultDto rtDto = new ResultDto();
		OaDictAttnEqpt ae = attenEquiService.get(id);
		int result = 0;
		if (ae != null) {
			result = attenEquiService.delete(id);
		}
		if (result <= 0) {
			rtDto.resultError();
			rtDto.setMessage("要删除的记录不存在!");
		}
		return rtDto;
	}

	/**
	 * 根据参数获取信息
	 * @param id
	 * @return
	 * ResultDto
	 */
	@RequestMapping("/get/{id}")
	@ResponseBody
	public ResultDto get(@PathVariable Integer id) {
		ResultDto rtDto = new ResultDto();
		rtDto.setData(attenEquiService.get(id));
		return rtDto;
	}
}
